package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	// 1. 99乘法表
	printMultiplicationTable()

	println("--------------------------------------")

	// 2. 随机生成20以内的20个非0正整数，进行累加和累乘操作
	rand.Seed(time.Now().UnixNano())
	numbers := generateNumbers(20)
	fmt.Println("生成的数值:", numbers)
	sum, product := calculate(numbers)
	fmt.Println("第单数个累加求和:", sum)
	fmt.Println("第偶数个累乘求积:", product)

	println("--------------------------------------")

	// 3. 打印100以内的斐波那契数列
	printFibonacci(100)
}

// 1. 打印99乘法表
func printMultiplicationTable() {
	for i := 1; i <= 9; i++ {
		for j := 1; j <= i; j++ {
			fmt.Printf("%d*%d=%d\t", j, i, i*j)
		}
		fmt.Println()
	}
}

// 2. 生成20个非0正整数
func generateNumbers(count int) []int {
	numbers := make([]int, count)
	for i := 0; i < count; i++ {
		numbers[i] = rand.Intn(19) + 1
	}
	return numbers
}

// 2. 对生成的数值进行操作
func calculate(numbers []int) (int, int) {
	sum := 0
	product := 1

	for i, num := range numbers {
		if (i+1)%2 == 1 {
			sum += num
		} else {
			product *= num
		}
	}

	return sum, product
}

// 3. 打印斐波那契数列
func printFibonacci(limit int) {
	a, b := 0, 1

	fmt.Print("斐波那契数列: ")
	for a <= limit {
		fmt.Print(a, " ")

		a, b = b, a+b
	}
	fmt.Println()
}
